package otherapi;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StopWatch;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 8/3/2022 3:59 PM
 */
public class StopWatchTest {

    private static final Logger logger = LoggerFactory.getLogger(StopWatchTest.class);

    @Test
    public void test() throws InterruptedException {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();                                //计时开始
        Thread.sleep(100);
        stopWatch.stop();                                  //计时结束
        //总用时
        logger.info(String.valueOf(stopWatch.getTotalTimeMillis()));
    }

    @Test
    public void test1() throws InterruptedException {
        StopWatch sw = new StopWatch("test");
        sw.start("task1");
        // do something
        Thread.sleep(100);
        sw.stop();
        sw.start("task2");
        // do something
        Thread.sleep(200);
        sw.stop();
        logger.info(sw.prettyPrint());
    }

    @Test
    public void test2() throws InterruptedException {
        StopWatch sw1 = new StopWatch("test1");
        StopWatch sw2 = new StopWatch("test2");
        int num = 0;
        sw1.start();
        for (int i = 0; i < 150000; i++) {
            if (num == 10000) {
                Thread.sleep(1000);
                sw1.stop();
                logger.info(String.valueOf(sw1.getTotalTimeMillis()));
                num = 0;
                sw1.start();
            }
            num++;
        }
    }
}
